﻿using System;
using System.Collections.Generic;
using System.Globalization;

namespace Truncation.Detector.Win32.Controls
{
    class Button : Control
    {
        public Button()
        {
            LeftPadding = RightPadding = 3;
            TopPadding = BottomPadding = 2;
        }
        public override bool Measure(IntPtr hdc, double scale = 1.0)
        {
            bool ret = base.Measure(hdc, scale);
            if (ret)
                return true;
            var txt = Content.Replace("\\n", "\n");
            var clientWidth = Width - LeftPadding - RightPadding;
            var clientHeight = Height - TopPadding - BottomPadding;
            TextElementEnumerator charEnum = StringInfo.GetTextElementEnumerator(txt);

            int totalH = 0;
            int fontHeight = MeasureText(hdc, txt.Split('\n')[0], scale).Height;
            string word = String.Empty;
            var blocks = new List<string>();
            while (charEnum.MoveNext())
            {
                string textElement = charEnum.GetTextElement();
                int unicode = textElement.Length == 2 ? (textElement[0] << 16 | textElement[1]) : textElement[0];
                if (unicode < 32 && unicode > 0)
                {
                    // non-printable character
                    if (unicode == 10 || unicode == 13)
                    {
                        blocks.Add(textElement);
                    }
                    word = String.Empty;
                    continue;
                }
                if (unicode == 32)
                {
                    if (String.IsNullOrWhiteSpace(word))
                        word += textElement;
                    else
                    {
                        blocks.Add(word);
                        word = textElement;
                    }
                    continue;
                }
                if (String.IsNullOrWhiteSpace(word))
                {
                    if (!String.IsNullOrEmpty(word))
                        blocks.Add(word);
                    word = textElement;
                }
                else
                {
                    word += textElement;
                }
            }
            if (!String.IsNullOrWhiteSpace(word))
            {
                blocks.Add(word);
            }
            string sentence = String.Empty;
            foreach (string block in blocks)
            {
                if (block == "\n" || block == "\r")
                {
                    sentence = String.Empty;
                    totalH += fontHeight;
                    continue;
                }
                if (String.IsNullOrWhiteSpace(block))
                {
                    sentence += block;
                    continue;
                }
                word = sentence + block;
                var size = MeasureText(hdc, word, scale);
                if (size.Width > clientWidth)
                {
                    sentence = block;
                    totalH += fontHeight;
                }
                else
                {
                    sentence = word;
                }
            }
            if (totalH >= clientHeight)
                return false;
            return true;
        }
    }
}
